import ctns from './constants.js';
let util = {
    
};
util.title = function(title) {
    title = title ? title + ' - Home' : '医链云开放平台';
    window.document.title = title;
};

util.deepCopy = function(source) {
    var result = {};
    for (var key in source) {
        if (source[key] == null) {
            result[key] = null;
        } else {
            result[key] = typeof source[key] === 'object' ? this.deepCopy(source[key]) : source[key];
        }

    }
    return result;
};
util.decode = function(val, codetype) {
    var res = ctns[codetype][val];
    if (typeof(res) == 'undefined') {
        res = val;
    }
    return res;
};
util.formatDate = function(val, fmt) {
    if (Object.prototype.toString.call(val) === "[object String]") {
        let date = new Date(val);
        return privateFormatDate(date, fmt);
    } else {
        return privateFormatDate(val, fmt);
    }
}

function privateFormatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
};

export default util;